package com.pocketsweet.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.UpdatePasswordCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.HeaderLayout.onRightContainerListener;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

/**
 * @author hantian 取回密码之后创建新的密码
 */
@ContentView(R.layout.activity_forgetpwd_create)
public class CreateNewPassword extends Activity {
	@ViewInject(R.id.etPassword)
	private EditText etPassword;

	@ViewInject(R.id.btnShowPwd)
	private ImageView btnShowPwd;

	@ViewInject(R.id.mainHeader)
	private HeaderLayout header;
	private boolean isShowPwd = false;
	private String smsCode;
	private long mExitTime = 0;// 退出事件用到的事件

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		smsCode = intent.getStringExtra("smsCode");
		ViewUtils.inject(this);
		initEdit();
		setHeaderFunction();
	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("下一步");
		header.setLeftText("");
		header.setMiddleText("创建新密码");
		header.setRighttContainerClickListener(new onRightContainerListener() {

			@Override
			public void onClick() {
				String password = etPassword.getText().toString();
				if (TextUtils.isEmpty(password) || password.length() < 6
						|| password.length() > 16) {
					ToolKits.toast(CreateNewPassword.this,
							R.string.password_hint);
					return;
				}
				MLUser.resetPasswordBySmsCodeInBackground(smsCode, password,
						new UpdatePasswordCallback() {
							@Override
							public void done(AVException e) {
								if (e == null) {
									// 发送成功了
									ToolKits.toast(CreateNewPassword.this,
											"修改成功");
									Intent loginIntent = new Intent(
											CreateNewPassword.this, TempMain.class);
									CreateNewPassword.this
											.startActivity(loginIntent);
									CreateNewPassword.this.finish();
								} else {
									ToolKits.toast(CreateNewPassword.this,
											e.getCode() + e.getMessage());
									return;
								}
							}
						});

			}
		});
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				Intent intent = new Intent(CreateNewPassword.this,
						ForgetPassword.class);
				intent.putExtra("smsCode", smsCode);
				CreateNewPassword.this.startActivity(intent);
				CreateNewPassword.this.finish();
			}
		});
	}

	private void initEdit() { // 监听输入框
		etPassword.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if (!StringUtils.isEmpty(etPassword.getText().toString())) {
					btnShowPwd.setVisibility(View.VISIBLE);
					btnShowPwd.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							if (isShowPwd) {
								isShowPwd = false;
								// 设置密码为隐藏的
								btnShowPwd
										.setBackgroundDrawable(getResources()
												.getDrawable(
														R.drawable.icon_pwd_unshow));
								etPassword
										.setTransformationMethod(PasswordTransformationMethod
												.getInstance());
								if (etPassword.length() > 0) {
									etPassword.setSelection(etPassword.length());
								}
							} else { // 设置EditText的密码为可见的
								isShowPwd = true;
								btnShowPwd.setBackgroundDrawable(getResources()
										.getDrawable(R.drawable.icon_pwd_show));
								etPassword
										.setTransformationMethod(HideReturnsTransformationMethod
												.getInstance());
								if (etPassword.length() > 0) {
									etPassword.setSelection(etPassword.length());
								}

							}
						}
					});
				} else {
					btnShowPwd.setVisibility(View.GONE);
				}

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}
